using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace INTEX.DataBase.CE
{
    public partial class Mapper
    {
    /*
      var qVAART = from m in mtemp.VAART select m;
       DBMapper.MapVAART(qVAART.ToList(), true);
       */
        public List<VAART> MapVAART(XElement item2, bool mergedb)
        {
            List<VAART> result = new List<VAART>();
            var item4 = from item3 in item2.Elements()
                        select new VAART
                        {  
                            MAINKEY = (string)item3.Element("MAINKEY"),
                            VAAMANDANT = (string)item3.Element("VAAMANDANT"),
                            VAAKOLL = (string)item3.Element("VAAKOLL"),
                            VAAART = (string)item3.Element("VAAART"),
                            VAABEZ = (string)item3.Element("VAABEZ"),
                            VAAMEBRT = (string)item3.Element("VAAMEBRT"),
                            VAABRTVON = (string)item3.Element("VAABRTVON"),
                            VAADART = (string)item3.Element("VAADART"),
                            VAADVAR = (string)item3.Element("VAADVAR"),
                            VAAARTST = (string)item3.Element("VAAARTST"),
                            PHOTO = (string)item3.Element("PHOTO")
                        }; 
                        
          result = item4.ToList();
          MapVAART(result, mergedb);
          return result;
        }
        ///DBMapper.MapVAART(mtemp.VAART.ToList<VAART>(), true);        
       public void MapVAART(List<VAART> result, bool mergedb)
        {
          if (mergedb)
            {     
            
                int loaded = 0;
                int page = 500;
                int count = result.Count();
                while (true)
                {
                    if (count - loaded < page) 
                    {
                        page = count - loaded;
                    }
                    var query =
                        (from VAART row in result.AsQueryable()
                         select row).Skip(loaded).Take(page);
               var rowsexist = from u in LocalDB.VAART.ToList().AsQueryable()
                                           join uc in query.AsQueryable() on  u.MAINKEY equals  uc.MAINKEY
                                           select u;
                LocalDB.VAART.DeleteAllOnSubmit(rowsexist);
                LocalDB.SubmitChanges();
                LocalDB.VAART.InsertAllOnSubmit(query);
                LocalDB.SubmitChanges();
                 loaded += page;
                if (count == loaded)
                        break;
                }
            }
       }
    }
    [Table(Name = "VAART")]
    public class VAART : TBBASE
    {
        private string _MAINKEY = "0";
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public string MAINKEY
        {
            get
            {
                return _MAINKEY;
            }
            set
            {
                if (value != _MAINKEY)
                {
                    _MAINKEY = value;
                    NotifyPropertyChanging("MAINKEY");                 
                }
            }
        }
        private string _Photo = "YES";
        [Column]
        public string PHOTO
        {
            get
            {
                return _Photo;
            }
            set
            {
                if (value != _Photo)
                {
                    _Photo = value;
                    NotifyPropertyChanging("PHOTO");
                }
            }
        } 
        [Column(IsVersion = true)]
        private Binary _version;
   
        private string  _VAAMANDANT="0";
        [Column]
        public string VAAMANDANT
        {
            get
            {
                return _VAAMANDANT;
            }
            set
            {
                if (value != _VAAMANDANT)
                {
                     _VAAMANDANT = value;
                    NotifyPropertyChanged("VAAMANDANT");
                }
            }
        }
        private string  _VAAKOLL="0";
        [Column]
        public string VAAKOLL
        {
            get
            {
                return _VAAKOLL;
            }
            set
            {
                if (value != _VAAKOLL)
                {
                     _VAAKOLL = value;
                    NotifyPropertyChanged("VAAKOLL");
                }
            }
        }
        private string  _VAAART="0";
        [Column]
        public string VAAART
        {
            get
            {
                return _VAAART;
            }
            set
            {
                if (value != _VAAART)
                {
                     _VAAART = value;
                    NotifyPropertyChanged("VAAART");
                }
            }
        }
        private string  _VAABEZ="0";
        [Column]
        public string VAABEZ
        {
            get
            {
                return _VAABEZ;
            }
            set
            {
                if (value != _VAABEZ)
                {
                     _VAABEZ = value;
                    NotifyPropertyChanged("VAABEZ");
                }
            }
        }
        private string  _VAAMEBRT="0";
        [Column]
        public string VAAMEBRT
        {
            get
            {
                return _VAAMEBRT;
            }
            set
            {
                if (value != _VAAMEBRT)
                {
                     _VAAMEBRT = value;
                    NotifyPropertyChanged("VAAMEBRT");
                }
            }
        }
        private string  _VAABRTVON="0";
        [Column]
        public string VAABRTVON
        {
            get
            {
                return _VAABRTVON;
            }
            set
            {
                if (value != _VAABRTVON)
                {
                     _VAABRTVON = value;
                    NotifyPropertyChanged("VAABRTVON");
                }
            }
        }
        private string  _VAADART="0";
        [Column]
        public string VAADART
        {
            get
            {
                return _VAADART;
            }
            set
            {
                if (value != _VAADART)
                {
                     _VAADART = value;
                    NotifyPropertyChanged("VAADART");
                }
            }
        }
        private string  _VAADVAR="0";
        [Column]
        public string VAADVAR
        {
            get
            {
                return _VAADVAR;
            }
            set
            {
                if (value != _VAADVAR)
                {
                     _VAADVAR = value;
                    NotifyPropertyChanged("VAADVAR");
                }
            }
        }
        private string  _VAAARTST="0";
        [Column]
        public string VAAARTST
        {
            get
            {
                return _VAAARTST;
            }
            set
            {
                if (value != _VAAARTST)
                {
                     _VAAARTST = value;
                    NotifyPropertyChanged("VAAARTST");
                }
            }
        }
      public void ConvertToFile(IsolatedStorageFile ISO,ObservableCollection<VAART> ListInput, string filep = "", string setguid="")
        {           
            try
            {
                string localfile = filep;
                if (filep.Trim() == "")
                {
                    if (setguid.Trim() == "")
                        setguid = Guid.NewGuid().ToString();
                    localfile = "Data\\PhoneDB\\VAART\\VAART" +setguid + ".xml";
                }
                string folder = "Data\\PhoneDB\\VAART" ;
                if (!ISO.DirectoryExists(folder))
                {
                    ISO.CreateDirectory(folder);
                }
                ISO.DeleteFile(localfile);
                StreamWriter sw = new StreamWriter(ISO.CreateFile(localfile));
                XDocument xdoc = new XDocument();
                //Root Element
                XElement root = new XElement("VAART");
                if (setguid.Trim() != "")
                {
                    root = new XElement("ITX"+setguid);
                }
                //Add root to file
                xdoc.Add(root);
                foreach (VAART row in ListInput)
                {
                    XElement f = new XElement("VAART");
                    XElement MAINKEY = new XElement("MAINKEY");
                     f.Add(MAINKEY);
                   
                        XElement VAAMANDANT = new XElement("VAAMANDANT");
                         VAAMANDANT.Value = row.VAAMANDANT;
                          f.Add(VAAMANDANT);
                        XElement VAAKOLL = new XElement("VAAKOLL");
                         VAAKOLL.Value = row.VAAKOLL;
                          f.Add(VAAKOLL);
                        XElement VAAART = new XElement("VAAART");
                         VAAART.Value = row.VAAART;
                          f.Add(VAAART);
                        XElement VAABEZ = new XElement("VAABEZ");
                         VAABEZ.Value = row.VAABEZ;
                          f.Add(VAABEZ);
                        XElement VAAMEBRT = new XElement("VAAMEBRT");
                         VAAMEBRT.Value = row.VAAMEBRT;
                          f.Add(VAAMEBRT);
                        XElement VAABRTVON = new XElement("VAABRTVON");
                         VAABRTVON.Value = row.VAABRTVON;
                          f.Add(VAABRTVON);
                        XElement VAADART = new XElement("VAADART");
                         VAADART.Value = row.VAADART;
                          f.Add(VAADART);
                        XElement VAADVAR = new XElement("VAADVAR");
                         VAADVAR.Value = row.VAADVAR;
                          f.Add(VAADVAR);
                        XElement VAAARTST = new XElement("VAAARTST");
                         VAAARTST.Value = row.VAAARTST;
                          f.Add(VAAARTST);
                  root.Add(f);
                }
                sw.Write(xdoc);
                sw.Close();
                sw.Dispose();
            }
            catch (Exception ex)
            { 
            }
      
        }                                        
    }
}
